package com.x.service.api;

import com.x.service.entity.AutoComplete;
import com.x.service.entity.Base;
import com.x.service.entity.BookDetail;
import com.x.service.entity.BookList;
import com.x.service.entity.CatsList;
import com.x.service.entity.Chapter;
import com.x.service.entity.ChapterList;
import com.x.service.entity.ChaptersInfo;
import com.x.service.entity.DeviceInfo;
import com.x.service.entity.HotWord;
import com.x.service.entity.RangeList;
import com.x.service.entity.TagList;
import com.x.service.entity.Version;
import com.x.service.entity.map.MapManger;
import com.x.zssqservice.api.BookApi;
import com.x.zssqservice.bean.BookListTags;
import com.x.zssqservice.bean.BookMixAToc;
import com.x.zssqservice.bean.BooksByCats;
import com.x.zssqservice.bean.BooksByTag;
import com.x.zssqservice.bean.CategoryList;
import com.x.zssqservice.bean.ChapterRead;
import com.x.zssqservice.bean.RankingList;
import com.x.zssqservice.bean.Rankings;
import com.x.zssqservice.bean.RecommendBookList;
import com.x.zssqservice.bean.SearchDetail;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * Created by H on 18/5/9.
 */

public class ZssqApi implements DataRepository {
    com.x.zssqservice.api.BookApi bookApi;
    com.x.service.api.BookApi xBookApi;
    MapManger mapManger;

    public ZssqApi(BookApi bookApi, com.x.service.api.BookApi xBookApi, MapManger mapManger) {
        this.bookApi = bookApi;
        this.xBookApi = xBookApi;
        this.mapManger = mapManger;
    }

    @Override
    public Observable<BookList> getRecommendList(String bookId) {
        return bookApi.getRecommendBookList(bookId, "20").concatMap(new Func1<RecommendBookList, Observable<? extends BookList>>() {
            @Override
            public Observable<? extends BookList> call(RecommendBookList recommendBookList) {
                return Observable.create(new Observable.OnSubscribe<BookList>() {
                    @Override
                    public void call(Subscriber<? super BookList> subscriber) {
                        subscriber.onNext(mapManger.recommendLstTpBookList.map(recommendBookList));
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    @Override
    public Observable<BookDetail> getBookDetial(String bookId) {
        return bookApi.getBookDetail(bookId).concatMap(new Func1<com.x.zssqservice.bean.BookDetail, Observable<? extends BookDetail>>() {
            @Override
            public Observable<? extends BookDetail> call(com.x.zssqservice.bean.BookDetail bookDetail) {
                return Observable.create(new Observable.OnSubscribe<BookDetail>() {
                    @Override
                    public void call(Subscriber<? super BookDetail> subscriber) {
                        subscriber.onNext(mapManger.bookDetialMap.map(bookDetail));
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    @Override
    public Observable<BookList> getBooksByTag(TagList.Tag tag, int start, int limit) {
        return bookApi.getBooksByTag(tag.name, start + "", limit + "").concatMap(new Func1<BooksByTag, Observable<? extends BookList>>() {
            @Override
            public Observable<? extends BookList> call(BooksByTag booksByTag) {
                return Observable.create(new Observable.OnSubscribe<BookList>() {
                    @Override
                    public void call(Subscriber<? super BookList> subscriber) {
                        subscriber.onNext(mapManger.booksByTagToBookList.map(booksByTag));
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    @Override
    public Observable<BookList> getBooksByAuthor(String author, int start, int limit) {
        return bookApi.searchBooksByAuthor(author).concatMap(new Func1<BooksByTag, Observable<? extends BookList>>() {
            @Override
            public Observable<? extends BookList> call(BooksByTag booksByTag) {
                return Observable.create(new Observable.OnSubscribe<BookList>() {
                    @Override
                    public void call(Subscriber<? super BookList> subscriber) {
                        subscriber.onNext(mapManger.booksByTagToBookList.map(booksByTag));
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    @Override
    public Observable<BookList> searchBook(String query, int start, int limit) {
        return bookApi.getSearchResult(query).concatMap(new Func1<SearchDetail, Observable<? extends BookList>>() {
            @Override
            public Observable<? extends BookList> call(SearchDetail searchDetail) {
                return Observable.create(new Observable.OnSubscribe<BookList>() {
                    @Override
                    public void call(Subscriber<? super BookList> subscriber) {
                        subscriber.onNext(mapManger.searchDetailToBookList.map(searchDetail));
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    @Override
    public Observable<AutoComplete> autoComplete(String query) {
        return bookApi.getAutoComplete(query).concatMap(new Func1<com.x.zssqservice.bean.AutoComplete, Observable<? extends AutoComplete>>() {
            @Override
            public Observable<? extends AutoComplete> call(com.x.zssqservice.bean.AutoComplete autoComplete) {
                return Observable.create(new Observable.OnSubscribe<AutoComplete>() {
                    @Override
                    public void call(Subscriber<? super AutoComplete> subscriber) {
                        subscriber.onNext(mapManger.autoCompleteMap.map(autoComplete));
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    @Override
    public Observable<HotWord> getHotWord() {
        return bookApi.getHotWord().concatMap(new Func1<com.x.zssqservice.bean.HotWord, Observable<? extends HotWord>>() {
            @Override
            public Observable<? extends HotWord> call(com.x.zssqservice.bean.HotWord hotWord) {
                return Observable.create(new Observable.OnSubscribe<HotWord>() {
                    @Override
                    public void call(Subscriber<? super HotWord> subscriber) {
                        subscriber.onNext(mapManger.hotWordMap.map(hotWord));
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    @Override
    public Observable<ChaptersInfo> getChapters(String bookId) {
        return bookApi.getBookMixAToc(bookId, "chapters").concatMap(new Func1<BookMixAToc, Observable<? extends ChaptersInfo>>() {
            @Override
            public Observable<? extends ChaptersInfo> call(BookMixAToc bookMixAToc) {
                return Observable.create(new Observable.OnSubscribe<ChaptersInfo>() {
                    @Override
                    public void call(Subscriber<? super ChaptersInfo> subscriber) {
                        subscriber.onNext(mapManger.bookMixATocToChapterList.map(bookMixAToc));
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    @Override
    public Observable<Chapter> getChapterRead(String link) {
        return bookApi.getChapterRead(link).concatMap(new Func1<ChapterRead, Observable<? extends Chapter>>() {
            @Override
            public Observable<? extends Chapter> call(ChapterRead chapterRead) {
                return Observable.create(new Observable.OnSubscribe<Chapter>() {
                    @Override
                    public void call(Subscriber<? super Chapter> subscriber) {
                        subscriber.onNext(mapManger.chapterReadToChapter.map(chapterRead));
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    @Override
    public Observable<TagList> getTagLst() {
        return bookApi.getBookListTags().concatMap(new Func1<BookListTags, Observable<? extends TagList>>() {
            @Override
            public Observable<? extends TagList> call(BookListTags bookListTags) {
                return Observable.create(new Observable.OnSubscribe<TagList>() {
                    @Override
                    public void call(Subscriber<? super TagList> subscriber) {
                        subscriber.onNext(mapManger.bookListTagsToTagList.map(bookListTags));
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    @Override
    public Observable<CatsList> getCatList() {
        return bookApi.getCategoryList().concatMap(new Func1<CategoryList, Observable<? extends CatsList>>() {
            @Override
            public Observable<? extends CatsList> call(CategoryList categoryList) {
                return Observable.create(new Observable.OnSubscribe<CatsList>() {
                    @Override
                    public void call(Subscriber<? super CatsList> subscriber) {
                        subscriber.onNext(mapManger.categoryListToCatsList.map(categoryList));
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    @Override
    public Observable<BookList> getBookByCat(CatsList.Cat cat, int start, int limit) {
        return bookApi.getBooksByCats("male", "", cat.name, "", start, limit).concatMap(new Func1<BooksByCats, Observable<? extends BookList>>() {
            @Override
            public Observable<? extends BookList> call(BooksByCats booksByCats) {
                return Observable.create(new Observable.OnSubscribe<BookList>() {
                    @Override
                    public void call(Subscriber<? super BookList> subscriber) {
                        subscriber.onNext(mapManger.booksByCatsToBookList.map(booksByCats));
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    @Override
    public Observable<RangeList> getRangelist() {
        return bookApi.getRanking().concatMap(new Func1<RankingList, Observable<? extends RangeList>>() {
            @Override
            public Observable<? extends RangeList> call(RankingList rankingList) {
                return Observable.create(new Observable.OnSubscribe<RangeList>() {
                    @Override
                    public void call(Subscriber<? super RangeList> subscriber) {
                        subscriber.onNext(mapManger.rankingListToRangeList.map(rankingList));
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    @Override
    public Observable<BookList> getBooksByRangeId(String id) {
        return bookApi.getRanking(id).concatMap(new Func1<Rankings, Observable<? extends BookList>>() {
            @Override
            public Observable<? extends BookList> call(Rankings rankings) {
                return Observable.create(new Observable.OnSubscribe<BookList>() {
                    @Override
                    public void call(Subscriber<? super BookList> subscriber) {
                        subscriber.onNext(mapManger.rankingsToBookList.map(rankings));
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    @Override
    public Observable<Base> reportDerice(DeviceInfo deviceInfo) {
        return xBookApi.reportDerice(deviceInfo);
    }

    @Override
    public Observable<Version> getVersion(String channelId) {
        return xBookApi.getVersion(channelId);
    }

    @Override
    public Observable<Base> addToLib(String token, String book) {
        return xBookApi.addToLib(token, book);
    }

    @Override
    public Observable<BookList> getBooksByIds(String bookId) {
        return xBookApi.getBooksByIds(bookId);
    }
}
